from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField, SubmitField, BooleanField
from wtforms.validators import DataRequired, Length, ValidationError
from application.users.models import *


# 注册表单
class RegisterForm(FlaskForm):
    name = StringField('昵称', validators=[DataRequired(), Length(max=128, message='用户名不能超过128个字')])
    mobile = StringField('手机', validators=[DataRequired(), Length(max=32, message='手机号不能超过32位')])
    password = PasswordField('密码', validators=[DataRequired(), Length(min=6, max=32, message='密码不能小于6位')])
    user_type = StringField('秘钥', validators=[Length(max=128, message='秘钥不能超过128个字')])
    submit = SubmitField('提交')

    # 验证手机号是否存在
    def validate_mobile(self, field):
        mobile = field.data
        if Users.query.filter_by(mobile=mobile).first():
            raise ValidationError('该手机号已存在')


# 登录表单
class LoginForm(FlaskForm):
    name = StringField('用户名/手机号', validators=[DataRequired(message='用户名不能为空')])
    password = PasswordField('密码', validators=[DataRequired(message='密码不能为空')])

    submit = SubmitField('登录')